package com.elephant.core.socket.socket2;

import java.io.*;
import java.net.Socket;

/**
 * 2、客户端和服务端同时读和写
 * @author hai
 * @description
 * @date 10:57 2018/8/31
 */
public class Client {

    public static void main(String[] args) throws IOException {

        String host = "127.0.0.1";
        Integer port = 8888;
        Socket socket = new Socket(host,port);
        Writer writer = new OutputStreamWriter(socket.getOutputStream());
        writer.write("hello world");
        writer.flush();

        Reader reader = new InputStreamReader(socket.getInputStream());
        char[] chars = new char[64];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = reader.read(chars)) != -1){
            sb.append(new String(chars,0,len));
        }
        System.out.println("From Server" + sb);

        writer.close();
        reader.close();
        socket.close();
    }
}
